<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-form :form="form">
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="工作站名称"
      >
        <a-input
          placeholder="请输入工作站名称"
          v-decorator="['workName', { rules: [{ required: true, message: '请输入工作站名称!' }] }]"
          maxLength="15"
        />
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="工作站类型"
      >
        <a-select
          placeholder="请选择"
          v-decorator="['terminalType']"
        >
          <a-select-option :value="0">电脑终端</a-select-option>
          <a-select-option :value="1">移动终端</a-select-option>
          <a-select-option :value="10">其他</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="管理车道"
      >
        <div>
          <a-checkbox
            :checked="checkAllArm"
            @change="onCheckAllChange"
            :disabled="checkableList.length===0"
          >全选</a-checkbox>
        </div>
        <a-checkbox-group
          @change="onChange"
          v-model="checkedArmList"
          :options="armList"
        >
        </a-checkbox-group>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="备注"
      >
        <a-input
          placeholder="可输入备注信息"
          v-decorator="['remark',{}]"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import ConfigSettingModalVue from '../../ConfigSettingModal.vue'
import { getAction } from '../../../../api/manage'

export default {
  name: 'ParkWorkStationModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      result: {},
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: '/parking/parkWorkStation/add',
        edit: '/parking/parkWorkStation/edit',
        parkArmInfoListStatusOk: '/parking/parkArmInfo/parkArmInfoListStatusOk',
        queryById: '/parking/parkWorkStation/queryById',
      },
      armList: [],
      checkedArmList: [],
      defaultCheckedArmList: [],
      checkAllArm: false,
      parkArmEntityList: [],
      checkableList: [],
    }
  },
  created() {},
  mounted() {
    let _self = this
    _self.init()
  },
  methods: {
    init() {},
    add() {
      this.edit({ terminalType: 0 })
    },
    queryById() {
      return new Promise((resolve) => {
        if (this.model.id) {
          //后台获取详细信息
          getAction(this.url.queryById, { id: this.model.id }).then((ret) => {
            if (ret.success) {
              resolve(ret.result.parkArmEntityList)
            }
          })
        } else {
          httpAction(this.url.parkArmInfoListStatusOk, {}, 'GET').then((res) => {
            if (res.code === 0) {
              resolve(res.result)
            }
          })
        }
      })
    },
    edit(record) {
      console.log(record)
      this.checkAllArm = false;
      this.form.resetFields()
      this.model = Object.assign({}, record)
      console.log(this.model.id)
      this.queryById().then((parkArmEntityList) => {
        if(parkArmEntityList===null){
          parkArmEntityList=[];
        }
        this.armList = parkArmEntityList.map((item) => {
          return Object.assign(item, {
            label: item.armName,
            value: item.id,
            disabled: !!item.workStationId && item.workStationId !== this.model.id,
          })
        })
        this.checkedArmList = parkArmEntityList
          .filter((item) => item.workStationId === this.model.id)
          .map((item) => item.id)
        this.checkableList = parkArmEntityList.filter(
          (item) => !item.workStationId || item.workStationId === this.model.id
        )
        console.log(this.checkedArmList)
        console.log(this.checkableList)
        console.log(this.checkedArmList.length === this.checkableList.length && this.checkedArmList.length > 0)
        if (this.checkedArmList.length === this.checkableList.length && this.checkedArmList.length > 0) {
          this.checkAllArm = true
        }
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'workName', 'terminalType', 'remark'))
        })
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      console.log(this.model)
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.parkArmList = this.checkedArmList

          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    onCheckAllChange(e) {
      console.log(this.checkableList)
      Object.assign(this, {
        checkAllArm: e.target.checked,
        checkedArmList: e.target.checked ? this.checkableList.map((item) => item.id) : [],
      })
      console.log(this.checkedArmList)
      console.log(this.armList)
    },
    onChange(checkedList) {
      this.checkAllArm = checkedList.length === this.checkableList.length
      this.checkedArmList = checkedList
    },
  },
}
</script>

<style lang="less" scoped>
</style>
